package com.test.mytest.file;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.GridLayout;
import java.awt.FlowLayout;

public class FileContentSearchApp {

	private JFrame frame;
	private JTextField txt_directory;
	private JTextArea textArea;
	private JTextField txt_search;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					FileContentSearchApp window = new FileContentSearchApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FileContentSearchApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("查找文件内容");
		frame.setBounds(100, 100, 729, 386);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		
		
		JLabel label = new JLabel("文件");
		label.setBounds(11, 9, 36, 15);
		panel.add(label);
		
		txt_directory = new JTextField();
		txt_directory.setBounds(52, 6, 246, 21);
		panel.add(txt_directory);
		txt_directory.setColumns(40);
		
		JButton button = new JButton("选择");
		button.setBounds(303, 5, 57, 23);
		panel.add(button);
		
		JButton button_1 = new JButton("搜索");
		button_1.setBounds(365, 5, 57, 23);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchDirectory(txt_directory.getText());
			}
		});
		
		txt_search = new JTextField();
		panel.add(txt_search);
		txt_search.setColumns(10);
		panel.add(button_1);
		
		textArea = new JTextArea();
		JScrollPane jScrollPane = new JScrollPane(textArea);
		frame.getContentPane().add(jScrollPane, BorderLayout.CENTER);
	}

	protected void searchDirectory(String directtory) {
		File file = new File(directtory);
		if(file.isDirectory()){
			for (File file2 : file.listFiles()) {
				if(file2.isDirectory()){
					searchDirectory(file2.getAbsolutePath());
				}else{
					searchFile(file2);
				}
			}
		}else{
			searchFile(file);
		}
	}

	private void searchFile(File file) {
		System.out.println("正在搜索文件："+file.getAbsolutePath());
		try(BufferedReader br=new BufferedReader(new FileReader(file))) {
			String s=br.readLine();
			while (s!=null) {
				if(s.contains(txt_search.getText())){
					textArea.append(file.getAbsolutePath()+"\t\n");
					return;
				}
				s=br.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
